package cn.nawang.ebeim.api.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import cn.nawang.ebeim.api.bean.CustomerBean;
import cn.nawang.ebeim.api.comm.BaseQuery;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.domain.Customer;

/**
 * @ClassName: CustomerQueryImpl
 * @Description: TODO
 * @author 许炜毅
 * @date 2015年8月6日 下午5:12:55
 * @version v1.0
 */
@Repository
public class CustomerQueryImpl extends BaseQuery {

  private static String LOAD_USER_COUNT =
      "select count(u.id) from User u,Department d where u.departmentId=d.id";
  private static String LOAD_USER_LIST =
      "select " + "new cn.nawang.ebeim.api.vo.UserView(u.id,u.name,u.username,d.name,"
          + "d.id,u.mobile,u.email,u.createTime,u.status,u.customerId,u.role,u.labId,u.labType,"
          + "u.isFirstLogin,u.spaceSize,u.safeLevel,u.bindDevice,u.bindTime,u.bindDeviceName) "
          + "from User u,Department d where u.departmentId=d.id";
  private static final String LOAD_CUSTOMER_COUNT = "select count(c.id) from Customer c where 1=1 ";
  private static final String LOAD_CUSTOMER = "from Customer c where 1=1 ";
  private static final String GET_CUSTOMER = "from Customer c where c.id=:id";
  private static final String GET_CUSTOMER_BY_NAME = "from Customer c where c.name=:name";
  private static final String GET_CUSTOMER_BY_TERMINALID =
      "select c from Customer c,Terminal t where c.id =t.customerId and t.id=:terminalId )";

  /**
   * @author 许炜毅
   * @Description 查询用户数据列表
   * @param page
   * @return PageList
   * @date 2015年8月6日下午5:13:14
   *
   * @修改人
   * @修改日期
   */
  public PageList findUserList(Pages page) {
    PageList pl = null;
    try {
      String cod = joinSearchCondition(page);// 拼接查询条件
      pl = queryListByPaging(LOAD_USER_COUNT + cod, LOAD_USER_LIST + cod, page);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return pl;
  }

  /**
   * @author 许炜毅
   * @Description 根据公司名称获取信息
   * @param companyName
   * @return Customer
   * @date 2015年8月7日下午3:49:42
   *
   * @修改人
   * @修改日期
   */
  public Customer findCustomerByName(String companyName) {
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("name", companyName);
      List<Customer> customer = queryObject(GET_CUSTOMER_BY_NAME, paraMap);
      if (customer != null && !customer.isEmpty()) {
        return customer.get(0);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  public PageList findCustomerList(Pages page) {
    PageList pl = null;
    String order = " order by c.createTime desc";
    try {
      String cod = joinSearchCondition(page);// 拼接查询条件
      pl = this.queryListByPaging(LOAD_CUSTOMER_COUNT + cod + order, LOAD_CUSTOMER + cod + order,
          page);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return pl;
  }

  public Customer findCustomerById(String id) {
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("id", id);
      List<Customer> customer = queryObject(GET_CUSTOMER, paraMap);
      if (customer != null && !customer.isEmpty()) {
        return customer.get(0);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据终端编号查找客户对象
   * @param terminalId 终端编号
   * @return Customer 客户对象
   * @date Aug 19, 20159:57:37 AM
   *
   * @修改人
   * @修改日期
   */
  public Customer findCustomerByTerminalId(String terminalId) {
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("terminalId", terminalId);
      List<Customer> customer = queryObject(GET_CUSTOMER_BY_TERMINALID, paraMap);
      if (customer != null && !customer.isEmpty()) {
        return customer.get(0);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  public CustomerBean getCustomerBean(String id) {
    String hql = "select new cn.nawang.ebeim.api.bean.CustomerBean"
        + "(c.name,c.maxCopys,s.salesPackageName,u.username,u.name,u.mobile,u.email,c.createTime) from"
        + " Customer c,User u,SalesPackage s where c.id=u.customerId and s.id=c.salesPacakgeId"
        + " and c.id=:id";
    Map<String, Object> paraMap = new HashMap<String, Object>();
    paraMap.put("id", id);
    try {
      List<CustomerBean> customer = queryObject(hql, paraMap);
      if (customer != null && !customer.isEmpty()) {
        return customer.get(0);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }
}
